package com.sgb.kjwl.view.ui.activity.self.lease;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfLeaseOrderListBinding;
import com.sgb.kjwl.view.ui.fragment.self.LeaseOrderStateFragment;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.ArrayList;

/**
 * 出租订单列表
 */
public class SelfLeaseOrderListActivity extends AppActivity {
    private ActivitySelfLeaseOrderListBinding binding;
    //订单状态status 全部, 1待接单,2 待确认,3待付款，4支付中(租入才有)，5待运输，6待验收,7已完成,8已取消（交易关闭） ,9已拒绝（交易关闭），10待签约
    private String[] titles = {"全部", "待接单", "待确认", "待签约", "待付款", "待运输", "待验收", "交易完成", "交易关闭"};
    private ArrayList<Fragment> mFragments;
    private String type = "1";//1=租入,2=租出

    public static void start(Context context, String type) {
        start(context, null, type);
    }

    public static void start(Context context, Intent extras, String type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, SelfLeaseOrderListActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(SelfLeaseOrderListActivity.this, R.layout.activity_self_lease_order_list);
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
//        binding.editView.setOnClickListener(this);
//        binding.editSearch.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
//        binding.editSearch.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    closeKeybord();
//                    EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_CAR_PUBLISH_FILTER).setMessageData(binding.editSearch.getText().toString()));
//                }
//                return false;
//            }
//        });
    }

    @Override
    protected void init() {
        type = getIntent().getStringExtra("type");
        if ("1".equals(type)) {
            binding.topLayout.titleView.setText("租入订单");
        } else {
            binding.topLayout.titleView.setText("出租订单");
        }
        //订单状态status 1待接单,2 待确认,3待付款，4支付中，5待运输，6待验收,7已完成,8已取消 ,9已拒绝,10待签约 全部为空
        mFragments = new ArrayList<Fragment>();
        mFragments.add(LeaseOrderStateFragment.newInstance("", type));//全部
        mFragments.add(LeaseOrderStateFragment.newInstance("1", type));
        mFragments.add(LeaseOrderStateFragment.newInstance("2", type));
        String myCompanyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        if (!(TextUtils.isEmpty(myCompanyNo) || "0".equals(myCompanyNo))) {//非个人身份才有签约
            mFragments.add(LeaseOrderStateFragment.newInstance("10", type));
        }
        mFragments.add(LeaseOrderStateFragment.newInstance("3", type));
        if ("1".equals(type)) {//租入列表 才有 4支付中 的状态
            mFragments.add(LeaseOrderStateFragment.newInstance("4", type));
            //订单状态status 全部, 1待接单,2 待确认,3待付款，4支付中(租入才有)，5待运输，6待验收,7已完成,8已取消（交易关闭） ,9已拒绝（交易关闭）,10待签约
            titles = new String[]{"全部", "待接单", "待确认", "待签约", "待付款", "支付中", "待运输", "待验收", "交易完成", "交易关闭"};
            if (TextUtils.isEmpty(myCompanyNo) || "0".equals(myCompanyNo)) {//非个人身份才有签约
                titles = new String[]{"全部", "待接单", "待确认", "待付款", "支付中", "待运输", "待验收", "交易完成", "交易关闭"};
            }
        } else {
            if (TextUtils.isEmpty(myCompanyNo) || "0".equals(myCompanyNo)) {//非个人身份才有签约
                titles = new String[]{"全部", "待接单", "待确认", "待付款", "待运输", "待验收", "交易完成", "交易关闭"};
            }
        }

        mFragments.add(LeaseOrderStateFragment.newInstance("5", type));
        mFragments.add(LeaseOrderStateFragment.newInstance("6", type));
        mFragments.add(LeaseOrderStateFragment.newInstance("7", type));
        mFragments.add(LeaseOrderStateFragment.newInstance("8,9", type));

        SlidingFragmentPagerAdapter mAdapetr = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(mAdapetr);
        binding.viewpager.setOffscreenPageLimit(1);
        binding.tablayout.setupWithViewPager(binding.viewpager);
    }

}

